//
//  RHPlantTopoMEView2.m
//  RuiHeApp
//
//  Created by Yan Chen on 2025/1/6.
//

#import "RHPlantTopoMEView2.h"
#import "RHMePlantDetailModel.h"

#define line_Width 2
@interface RHPlantTopoMEView2()
@property (nonatomic, strong) UIImageView *solarImg;//光伏

@property (nonatomic, strong) UIImageView *chgImg;//充电桩

@property (nonatomic, strong) UIImageView *gridImg;//电网

@property (nonatomic, strong) UIImageView *meterImg;//电表

@property (nonatomic, strong) UIImageView *loadImg;//负载

@property (nonatomic, strong) UIImageView *generatorImg;//发电机

//光伏圆点
@property (nonatomic ,strong) UIView *solarDotView;
//电网圆点
@property (nonatomic ,strong) UIView *gridDotView;
//负载圆点
@property (nonatomic ,strong) UIView *loadDotView;
//电池圆点
@property (nonatomic ,strong) UIView *batteryDotView;
//发电机圆点
@property (nonatomic ,strong) UIView *generatorDotView;

//光伏动画
@property (nonatomic ,strong) UIBezierPath *solarPath;
//负载动画
@property (nonatomic ,strong) UIBezierPath *loadPath;
//电网
@property (nonatomic ,strong) UIBezierPath *gridPath;
//电池动画
@property (nonatomic ,strong) UIBezierPath *devicePath;
//充电桩动画
@property (nonatomic ,strong) UIBezierPath *chgPath;
//发电机动画
@property (nonatomic ,strong) UIBezierPath *generatorPath;

@property (nonatomic, strong) RHMePlantDetailModel *detailModel;
@end
@implementation RHPlantTopoMEView2

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (void)setDetailModel:(RHMePlantDetailModel *)detailModel productType:(ProductType)productType
{
    //测试
//    detailModel.load = @"22";
//    detailModel.gen = @"63";
//    detailModel.grid = @"66";
//    detailModel.pv = @"89";
    _detailModel = detailModel;
    
    self.solarDotView.hidden = YES;
    self.gridDotView.hidden = YES;
    self.loadDotView.hidden = YES;
    self.batteryDotView.hidden = YES;
    self.generatorDotView.hidden = YES;
    
    //光伏动画
    if([detailModel.pv floatValue]>0){
        self.solarDotView.hidden = NO;
        [self addAnimationWithPointView:self.solarDotView path:self.solarPath duration:3 animationName:@"solar"];
    }else{
        [self.solarDotView.layer removeAllAnimations];
    }
    
    
    //负载动画
    if([detailModel.load floatValue]>0){
        self.loadDotView.hidden = NO;
        [self addAnimationWithPointView:self.loadDotView path:self.loadPath duration:3 animationName:@"load"];
    }else{
        [self.loadDotView.layer removeAllAnimations];
    }
    
    //发电机动画
    if([detailModel.gen floatValue]>0){
        self.generatorDotView.hidden = NO;
        [self addAnimationWithPointView:self.generatorDotView path:self.generatorPath duration:3 animationName:@"gen"];
    }else{
        [self.generatorDotView.layer removeAllAnimations];
    }
    
    if (productType == ProductType_HE_BAT||productType == ProductType_HE_ALLINONE) {
        [self updateHeAnimate];
    }else{
        [self updateMeAnimate];
    }
}

//户用的电网和电池动画
-(void)updateHeAnimate
{
    //电池动画
    if([self.detailModel.battery floatValue]>0){
        self.batteryDotView.hidden = NO;
        [self addAnimationWithPointView:self.batteryDotView path:self.devicePath duration:3 animationName:@"battery"];
    }else if ([self.detailModel.battery floatValue] < 0){
        self.batteryDotView.hidden = NO;
        [self addAnimationWithPointView:self.batteryDotView path:self.devicePath.bezierPathByReversingPath duration:3 animationName:@"battery_rever"];
    }else{
        [self.batteryDotView.layer removeAllAnimations];
    }
    
    //电网动画  负:grid---->inverter    正:inverter -----> grid
     if([self.detailModel.grid floatValue]<0){
         self.gridDotView.hidden = NO;
         [self addAnimationWithPointView:self.gridDotView path:self.gridPath duration:3 animationName:@"grid"];
     }else if ([self.detailModel.grid floatValue]> 0){
         self.gridDotView.hidden = NO;
         [self addAnimationWithPointView:self.gridDotView path:self.gridPath.bezierPathByReversingPath duration:3 animationName:@"grid_rever"];
     }else{
         [self.gridDotView.layer removeAllAnimations];
     }
}

//微用的电网和电池动画
-(void)updateMeAnimate
{
    //电池动画
    if([self.detailModel.battery floatValue]<0){
        self.batteryDotView.hidden = NO;
        [self addAnimationWithPointView:self.batteryDotView path:self.devicePath duration:3 animationName:@"battery"];
    }else if ([self.detailModel.battery floatValue] > 0){
        self.batteryDotView.hidden = NO;
        [self addAnimationWithPointView:self.batteryDotView path:self.devicePath.bezierPathByReversingPath duration:3 animationName:@"battery_rever"];
    }else{
        [self.batteryDotView.layer removeAllAnimations];
    }
    
    //电网动画  负:grid---->inverter    正:inverter -----> grid
     if([self.detailModel.grid floatValue]>0){
         self.gridDotView.hidden = NO;
         [self addAnimationWithPointView:self.gridDotView path:self.gridPath duration:3 animationName:@"grid"];
     }else if ([self.detailModel.grid floatValue]< 0){
         self.gridDotView.hidden = NO;
         [self addAnimationWithPointView:self.gridDotView path:self.gridPath.bezierPathByReversingPath duration:3 animationName:@"grid_rever"];
     }else{
         [self.gridDotView.layer removeAllAnimations];
     }
}

- (void)setupUI{
    kWeakSelf;
    self.backgroundColor = UIColor.whiteColor;
    
    //光伏
    UIImageView *solarImg = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"me_plant_topo_solar"]];
    [self addSubview:solarImg];
    self.solarImg = solarImg;
    [solarImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(@(CGSizeMake(60, 60)));
        make.left.mas_offset(40);
        make.top.mas_offset(20);
    }];
    
    QMUILabel *solarTitleLab = [QMUILabel labelWithFont:kBOLDSYSTEMFONT(12) textColor:kCOLOR_HEX_A(@"#171A1D", 0.6)];
    solarTitleLab.text = @"光伏".rh_localized;
    [self addSubview:solarTitleLab];
    [solarTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(solarImg.mas_bottom).offset(5);
        make.centerX.equalTo(solarImg);
    }];
    
    
    //电网
    UIImageView *gridImg = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"me_plant_topo_grid"]];
    [self addSubview:gridImg];
    self.gridImg = gridImg;
    [gridImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(solarImg);
        make.centerY.equalTo(solarImg);
        make.right.mas_offset(-40);
    }];
    
    QMUILabel *gridTitleLab = [QMUILabel labelWithFont:kBOLDSYSTEMFONT(12) textColor:kCOLOR_HEX_A(@"#171A1D", 0.6)];
    gridTitleLab.text = @"电网".rh_localized;
    [self addSubview:gridTitleLab];
    [gridTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(gridImg.mas_bottom).offset(5);
        make.centerX.equalTo(gridImg);
    }];
    
    
    
    //发电机
    UIImageView *generatorImg = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"me_plant_topo_generator"]];
    [self addSubview:generatorImg];
    self.generatorImg = generatorImg;
    [generatorImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(solarImg);
        make.top.equalTo(solarImg.mas_bottom).offset(40);
        make.size.equalTo(solarImg);
    }];
    
    QMUILabel *generatorTitleLab = [QMUILabel labelWithFont:kBOLDSYSTEMFONT(12) textColor:kCOLOR_HEX_A(@"#171A1D", 0.6)];
    generatorTitleLab.text = @"发电机".rh_localized;
    generatorTitleLab.numberOfLines = 0;
    generatorTitleLab.textAlignment = NSTextAlignmentLeft;
    [self addSubview:generatorTitleLab];
    [generatorTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(generatorImg.mas_bottom).offset(5);
        make.centerX.equalTo(generatorImg);
    }];
    
    //负载
    UIImageView *loadImg = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"me_plant_topo_load"]];
    [self addSubview:loadImg];
    self.loadImg = loadImg;
    [loadImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(solarImg);
        make.centerX.equalTo(solarImg);
        make.top.equalTo(generatorImg.mas_bottom).offset(40);
    }];
    
    QMUILabel *loadTitleLab = [QMUILabel labelWithFont:kBOLDSYSTEMFONT(12) textColor:kCOLOR_HEX_A(@"#171A1D", 0.6)];
    loadTitleLab.text = @"负载".rh_localized;
    [self addSubview:loadTitleLab];
    [loadTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(loadImg.mas_bottom).offset(5);
        make.centerX.equalTo(loadImg);
        make.bottom.mas_offset(-60);
    }];
    
    //计量电表
    UIImageView *meterImg = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"me_plant_topo_meter"]];
    [self addSubview:meterImg];
    self.meterImg = meterImg;
    [meterImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_offset(0);
        make.top.equalTo(loadImg);
        make.size.equalTo(solarImg);
    }];
    
    QMUILabel *meterTitleLab = [QMUILabel labelWithFont:kBOLDSYSTEMFONT(12) textColor:kCOLOR_HEX_A(@"#171A1D", 0.6)];
    meterTitleLab.text = @"计量电表".rh_localized;
    meterTitleLab.numberOfLines = 0;
    meterTitleLab.textAlignment = NSTextAlignmentLeft;
    [self addSubview:meterTitleLab];
    [meterTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(meterImg.mas_right).offset(5);
        make.right.mas_offset(-5);
        make.centerY.equalTo(meterImg);
    }];
    
    
    //充电桩
    UIImageView *chgImg = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"me_plant_topo_chg"]];
    [self addSubview:chgImg];
    self.chgImg = chgImg;
    [chgImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(solarImg);
        make.centerX.equalTo(gridImg);
        make.centerY.equalTo(generatorImg);
      
    }];
    
    QMUILabel *chgTitleLab = [QMUILabel labelWithFont:kBOLDSYSTEMFONT(12) textColor:kCOLOR_HEX_A(@"#171A1D", 0.6)];
    chgTitleLab.text = @"充电桩".rh_localized;
    [self addSubview:chgTitleLab];
    [chgTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {

        make.top.equalTo(weakSelf.chgImg.mas_bottom).mas_offset(5);
        make.centerX.equalTo(weakSelf.chgImg);
    }];
    
    //------ 小圆点
    _solarDotView = [[UIView alloc] initWithFrame:CGRectMake(-200, 0, 8, 8)];
    [_solarDotView setBackgroundColor:kCOLOR_HEX_A(@"#05C79D", 1)];
    [_solarDotView.layer setCornerRadius:4];
    [self addSubview:_solarDotView];
    
    _gridDotView = [[UIView alloc] initWithFrame:CGRectMake(-200, 0, 8, 8)];
    [_gridDotView setBackgroundColor:kCOLOR_HEX_A(@"#05C79D", 1)];
    [_gridDotView.layer setCornerRadius:4];
    [self addSubview:_gridDotView];
    
    _loadDotView = [[UIView alloc] initWithFrame:CGRectMake(-200, 0, 8, 8)];
    [_loadDotView setBackgroundColor:kCOLOR_HEX_A(@"#05C79D", 1)];
    [_loadDotView.layer setCornerRadius:4];
    [self addSubview:_loadDotView];
    
    _batteryDotView = [[UIView alloc] initWithFrame:CGRectMake(-200, 0, 8, 8)];
    [_batteryDotView setBackgroundColor:kCOLOR_HEX_A(@"#05C79D", 1)];
    [_batteryDotView.layer setCornerRadius:4];
    [self addSubview:_batteryDotView];

    _generatorDotView = [[UIView alloc] initWithFrame:CGRectMake(-200, 0, 8, 8)];
    [_generatorDotView setBackgroundColor:kCOLOR_HEX_A(@"#05C79D", 1)];
    [_generatorDotView.layer setCornerRadius:4];
    [self addSubview:_generatorDotView];
    
}


- (void)drawRect:(CGRect)rect
{

    //-------- 连线  负载-->电表
    UIBezierPath *solar_meter = [UIBezierPath bezierPath];
    solar_meter.lineWidth = line_Width;
   
    [kCOLOR_HEX(@"#CDD0D6") setStroke];
    [solar_meter moveToPoint:CGPointMake(self.solarImg.right, self.solarImg.centerY+15)];
    [solar_meter addLineToPoint:CGPointMake(self.meterImg.centerX-8 , self.solarImg.centerY+15)];
    UIBezierPath *circle_solar = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.meterImg.centerX-8, self.solarImg.centerY+8+15) radius:8 startAngle:M_PI+M_PI_2 endAngle:M_PI*2 clockwise:YES];
    [solar_meter appendPath:circle_solar];
    [solar_meter addLineToPoint:CGPointMake(self.meterImg.centerX , self.meterImg.top+15)];
    [solar_meter stroke];
    self.solarPath = solar_meter;
 
    //-------- 连线  电表-->电网
    UIBezierPath *meter_grid = [UIBezierPath bezierPath];
    meter_grid.lineWidth = line_Width;
   
    [kCOLOR_HEX(@"#CDD0D6")  setStroke];
    [meter_grid moveToPoint:CGPointMake(self.meterImg.centerX, self.meterImg.top+15)];
    [meter_grid addLineToPoint:CGPointMake(self.meterImg.centerX , self.gridImg.centerY+8+15)];
    
    UIBezierPath *circle_grid = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.meterImg.centerX+8, self.gridImg.centerY+8+15) radius:8 startAngle:M_PI endAngle:M_PI+M_PI_2 clockwise:YES];
    [meter_grid appendPath:circle_grid];
    [meter_grid addLineToPoint:CGPointMake(self.gridImg.left , self.gridImg.centerY+15)];
    [meter_grid stroke];
    self.gridPath = meter_grid;
    
    
    //-------- 连线  电表-->负载
    UIBezierPath *meter_load = [UIBezierPath bezierPath];
    meter_load.lineWidth = line_Width;
    [kCOLOR_HEX(@"#CDD0D6") setStroke];
    [meter_load moveToPoint:CGPointMake(self.meterImg.left ,self.meterImg.centerY+15)];
    [meter_load addLineToPoint:CGPointMake(self.loadImg.right ,self.loadImg.centerY+15)];
    [meter_load stroke];
    self.loadPath = meter_load;
    
    //-------- 连线 电表-->充电桩
    UIBezierPath *meter_chg = [UIBezierPath bezierPath];
    meter_chg.lineWidth = line_Width;
    [kCOLOR_HEX(@"#CDD0D6") setStroke];
    [meter_chg moveToPoint:CGPointMake(self.meterImg.centerX, self.meterImg.bottom)];
    [meter_chg addLineToPoint:CGPointMake(self.meterImg.centerX , self.chgImg.centerY+15)];
    [meter_chg addLineToPoint:CGPointMake(self.chgImg.left , self.chgImg.centerY+15)];
    [meter_chg stroke];
    self.chgPath = meter_chg;
    
    //-------- 连线  电表--->设备
    UIBezierPath *meter_device = [UIBezierPath bezierPath];
    meter_device.lineWidth = line_Width;
    [kCOLOR_HEX(@"#CDD0D6") setStroke];
    [meter_device moveToPoint:CGPointMake(self.meterImg.centerX, self.meterImg.bottom )];
    [meter_device addLineToPoint:CGPointMake(self.meterImg.centerX, self.bottom)];
    [meter_device stroke];
    self.devicePath = meter_device;
    
    //-------- 连线  发电机--->电表
    UIBezierPath *gen_meter = [UIBezierPath bezierPath];
    gen_meter.lineWidth = line_Width;
    [kCOLOR_HEX(@"#CDD0D6") setStroke];
    [gen_meter moveToPoint:CGPointMake(self.generatorImg.right, self.generatorImg.centerY+15 )];
    [gen_meter addLineToPoint:CGPointMake(self.meterImg.centerX, self.generatorImg.centerY+15)];
    [gen_meter addLineToPoint:CGPointMake(self.meterImg.centerX, self.meterImg.top+15)];
    [gen_meter stroke];
    self.generatorPath = gen_meter;
}


-(void)addAnimationWithPointView:(UIView*)pointView path:(UIBezierPath*)path duration:(NSTimeInterval)duration animationName:(NSString*)name
{
    [pointView.layer removeAllAnimations];
    CAKeyframeAnimation *pointAnimation = [self createAnimationWithPath:path duration:duration];
    [pointView.layer addAnimation:pointAnimation forKey:name];
}

-(CAKeyframeAnimation*)createAnimationWithPath:(UIBezierPath*)path duration:(NSTimeInterval)duration
{
    CAKeyframeAnimation *keyFrameAni = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    keyFrameAni.path = path.CGPath;
    keyFrameAni.removedOnCompletion = NO;
    keyFrameAni.duration = duration;
    keyFrameAni.repeatCount = MAXFLOAT;
    keyFrameAni.calculationMode = kCAAnimationPaced;;
    
    return keyFrameAni;
}
@end
